//引入exress模块
const express = require('express');
//引入连接池模块
const pool = require('../pool.js');
//创建路由器对象
const r = express.Router();


//获取所有菜谱ID、菜谱名和用户ID的接口
r.get('/allmenus', (req, res) => {
    //SQL语句以获取文章分类表的数据
    let sql = 'SELECT menu_id,mtitle,uid FROM menus,useInf'
        //执行SQL语句
    pool.query(sql, (err, results) => {
        if (err) throw err;
        // 返回数据到客户端
        res.send({ message: 'ok', code: 200, results: results });
    })
})

    //获取特定菜谱数据的接口
r.get('/details', (req, res) => {
    let _menu_id = req.query.menu_id
    // SQL语句以获取特定菜谱的数据
    let sql = 'select mtitle,mpic,remark,tips,headimg,optime,author_id,uname from menus,userInf where menu_id=?'
        //执行SQL语句
    pool.query(sql,[_menu_id], (error, results) => {
        if (error) throw error;
        // 返回数据到客户端
        res.send({ message: 'ok', code: 200, result: results});
    });

})

//获取特定菜谱用料表的接口
r.get('/materials', (req, res) => {
	var _menu_id = req.query.menu_id
    // SQL语句以获取特定菜谱用料表数据
    let sql = 'SELECT mater_id,food,count FROM materials,menus where mmid=menu_id and menu_id=?'
        //执行SQL语句
    pool.query(sql, [_menu_id], (error, results) => {
        if (error) throw error;
        // 返回数据到客户端
        res.send({ message: 'ok', code: 200, result: results });
    });
})

//获取特定菜谱步骤表的接口
r.get('/steps', (req, res) => {
	var _menu_id = req.query.menu_id
    // SQL语句以获取特定菜谱步骤表数据
    let sql = 'SELECT step_id,sorder,spic,stexp,msid FROM steps,menus where menu_id=?'
        // 执行SQL语句
    pool.query(sql, [_menu_id], (error, results) => {
        if (error) throw error;
        // 返回数据到客户端
        res.send({ message: 'ok', code: 200, result: results });
    });
})


//导出路由器对象
module.exports=r;